package MySQL;
import java.sql.*;

public class DatabaseConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/supermarket";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "Zyy18723600725";

    public static Connection getConnection() {
        Connection connection = null;
        try {
            // 建立连接
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("数据库连接成功!");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败!");
        }
        return connection;
    }

    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
